<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="镇名称" prop="town">
                <el-input v-model="queryParams.town" placeholder="请输入镇名称" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="村或社区" prop="villageUnit">
                <el-input v-model="queryParams.villageUnit" placeholder="请输入村或社区" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item> -->
            <el-form-item label="村社区名称" prop="villageName" label-width="85px">
                <el-input v-model="queryParams.villageName" placeholder="请输入村社区名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="姓名" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <!-- <el-form-item label="当选党内职务" prop="partyJob" label-width="110px">
                <el-input v-model="queryParams.partyJob" placeholder="请输入当选党内职务" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="任现任职务时间" prop="currentJobTime" label-width="110px">
                <el-input v-model="queryParams.currentJobTime" placeholder="请输入任现任职务时间" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phone">
                <el-input v-model="queryParams.phone" placeholder="请输入联系方式" clearable @keyup.enter.native="handleQuery" />
            </el-form-item> -->
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['wanzhi:village:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['wanzhi:village:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['wanzhi:village:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['wanzhi:village:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="villageList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" align="center" prop="sortNum" />
            <el-table-column label="镇名称" align="center" prop="town" />
            <el-table-column label="村或社区" align="center" prop="villageUnit" />
            <el-table-column label="村社区名称" align="center" prop="villageName" />
            <el-table-column label="姓名" align="center" prop="name" />
            <!-- <el-table-column label="性别" align="center" prop="sex" />
            <el-table-column label="出生年月" align="center" prop="brithYearMonth" />
            <el-table-column label="年龄" align="center" prop="age" />
            <el-table-column label="学历" align="center" prop="education" />
            <el-table-column label="入党时间" align="center" prop="partyTime" />
            <el-table-column label="身份证号" align="center" prop="idCard" /> -->
            <el-table-column label="当选党内职务" align="center" prop="partyJob" />
            <el-table-column label="任现任职务时间" align="center" prop="currentJobTime" />
            <el-table-column label="联系方式" align="center" prop="phone" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['wanzhi:village:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['wanzhi:village:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改村居对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="序号" prop="sortNum">
                    <el-input v-model="form.sortNum" type="number" placeholder="请输入序号" />
                </el-form-item>
                <el-form-item label="镇名称" prop="town">
                    <el-input v-model="form.town" placeholder="请输入镇名称" />
                </el-form-item>
                <el-form-item label="村或社区" prop="villageUnit">
                    <el-input v-model="form.villageUnit" placeholder="请输入村或社区" />
                </el-form-item>
                <el-form-item label="村社区名称" prop="villageName">
                    <el-input v-model="form.villageName" placeholder="请输入村社区名称" />
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="form.name" placeholder="请输入姓名" />
                </el-form-item>
                <el-form-item label="出生年月" prop="brithYearMonth">
                    <el-input v-model="form.brithYearMonth" placeholder="请输入出生年月" />
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input v-model="form.age" placeholder="请输入年龄" />
                </el-form-item>
                <el-form-item label="学历" prop="education">
                    <el-input v-model="form.education" placeholder="请输入学历" />
                </el-form-item>
                <el-form-item label="入党时间" prop="partyTime">
                    <el-input v-model="form.partyTime" placeholder="请输入入党时间" />
                </el-form-item>
                <el-form-item label="身份证号" prop="idCard">
                    <el-input v-model="form.idCard" placeholder="请输入身份证号" />
                </el-form-item>
                <el-form-item label="党内职务
          职务" prop="partyJob">
                    <el-input v-model="form.partyJob" placeholder="请输入党内职务
          职务" />
                </el-form-item>
                <el-form-item label="村(居)委会职务" prop="villageJob">
                    <el-input v-model="form.villageJob" placeholder="请输入村(居)委会职务" />
                </el-form-item>
                <el-form-item label="任现任职务时间" prop="currentJobTime">
                    <el-input v-model="form.currentJobTime" placeholder="请输入任现任职务时间" />
                </el-form-item>
                <el-form-item label="联系方式" prop="phone">
                    <el-input v-model="form.phone" placeholder="请输入联系方式" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import { listVillage, getVillage, delVillage, addVillage, updateVillage } from "@/api/system/village";

export default {
    name: "Village",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 村居表格数据
            villageList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                town: null,
                villageUnit: null,
                villageName: null,
                name: null,
                sex: null,
                brithYearMonth: null,
                age: null,
                education: null,
                partyTime: null,
                idCard: null,
                partyJob: null,
                villageJob: null,
                currentJobTime: null,
                phone: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                sortNum: [
                    { required: true, message: "请输入序号", trigger: "blur" },
                ],
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询村居列表 */
        getList() {
            this.loading = true;
            listVillage(this.queryParams).then(response => {
                this.villageList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                town: null,
                villageUnit: null,
                villageName: null,
                name: null,
                sex: null,
                brithYearMonth: null,
                age: null,
                education: null,
                partyTime: null,
                idCard: null,
                partyJob: null,
                villageJob: null,
                currentJobTime: null,
                phone: null,
                createTime: null,
                updateTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加村居";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getVillage(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改村居";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateVillage(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addVillage(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除村居编号为"' + ids + '"的数据项？').then(function () {
                return delVillage(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('wanzhi/village/export', {
                ...this.queryParams
            }, `village_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
  